define(function (require, exports, module) {
	var zoomSelectSlider, 
		btnZoomOut,
		btnZoomIn,
		selectZoom,
		btnGoToFirstPage,
		btnGoToPreviousPage,
		btnGoToNextPage,	
		btnGoToLastPage,
		pageIndexInputSelect;	

	$(function(){   
		zoomSelectSlider = $("#zoomSelectSlider");
		btnZoomOut = $(".btnZoomOut");
		btnZoomIn = $(".btnZoomIn");
		selectZoom = $(".btnSelectZoom");
		btnGoToFirstPage = $("#btnGoToFirstPage").addClass('disabled');	
		btnGoToPreviousPage = $("#btnGoToPreviousPage").addClass('disabled');	
		btnGoToNextPage = $("#btnGoToNextPage").addClass('disabled');		
		btnGoToLastPage = $("#btnGoToLastPage").addClass('disabled');	
		pageIndexInputSelect = $("#pageIndexInputSelect");			
	});

	function _bindEventsForAppInstance() {
	    if (!WebPDF.ViewerInstance) {
	        return true;
	    }

	    /******************************************View*********************************************/
		$(".btn-actual-size").on("click", function(){			
			WebPDF.ViewerInstance.zoom(WebPDF.ZOOM_ACTUAL_SIZE);
	    	$(this).addClass("actualsize24");
	    	$(".btn-fit-width").removeClass("fitwidth16");
	    	$(".btn-fit-page").removeClass("fitpages");
			
		});
		$(".btn-fit-page").on("click", function () {		
			WebPDF.ViewerInstance.zoom(WebPDF.ZOOM_FIT_PAGE);
	    	$(this).addClass("fitpages");
	    	$(".btn-actual-size").removeClass("actualsize24");
	    	$(".btn-fit-width").removeClass("fitwidth16");
			
		});
		$(".btn-fit-width").on("click", function () {		
			WebPDF.ViewerInstance.zoom(WebPDF.ZOOM_FIT_WIDTH);
	    	$(this).addClass("fitwidth16");
	    	$(".btn-actual-size").removeClass("actualsize24");
	    	$(".btn-fit-page").removeClass("fitpages");
			
		});

		btnZoomOut.on("click", function () {
			WebPDF.ViewerInstance.zoom(WebPDF.ZOOM_OUT);
		});
		btnZoomIn.on("click", function () {
			WebPDF.ViewerInstance.zoom(WebPDF.ZOOM_IN);
		});

		selectZoom.on("change", function () {			
	    	var zoomVal = $(this).val();
	        var val = parseFloat(zoomVal);
	        if(val){
	        	zoomVal = val / 100;
	        }
	        WebPDF.ViewerInstance.zoom(zoomVal);			
		});

		/******************************************StatusBar*********************************************/
		pageIndexInputSelect.change(function(){
			if (!WebPDF) return;
		    var selectPage = pageIndexInputSelect.val();
		    WebPDF.ViewerInstance.goToPage(parseInt(selectPage));
		});
		
		btnGoToFirstPage.on("click", function () {
			if (!WebPDF) return;
			WebPDF.ViewerInstance.goToPage(0);
		});

		btnGoToPreviousPage.on("click", function () {
			if (!WebPDF) return;
			WebPDF.ViewerInstance.goToPrevPage();
		});
		btnGoToNextPage.on("click", function () {
			if (!WebPDF) return;
			WebPDF.ViewerInstance.goToNextPage();
		});

		btnGoToLastPage.on("click", function () {
			if (!WebPDF) return;
			WebPDF.ViewerInstance.goToPage(WebPDF.ViewerInstance.getPageCount() - 1);
		});

		zoomSelectSlider.slider({
			range: "max",
			min: 0,
			max: WebPDF.Config.defaults.zoomList.length + 1,
			value: 0,
			slide: function(event, ui) {				
				var zoomList = WebPDF.ViewerInstance.getCurrentScaleList();
				var zoomSelected = zoomList[ui.value];
				WebPDF.ViewerInstance.zoom(zoomSelected);				
			}
     	});

		WebPDF.ViewerInstance
		.on(WebPDF.EventList.TOOL_CHANGED, function(event, data) {				
		})
		.on(WebPDF.EventList.DOCVIEW_ZOOM_CHANGED, function(event, data) {
			var zoomMode = WebPDF.ViewerInstance.curZoomMode();
			if (zoomMode == WebPDF.ZOOM_FIT_WIDTH) {
				$(".fit-width").addClass("fwr-active-btn");
			} else {
				$(".fit-width").removeClass("fwr-active-btn");
			}

			WebPDF.ViewerInstance.canZoomIn() ? $(".btnZoomIn").removeClass("disabled") : $(".btnZoomIn").addClass("disabled");
			WebPDF.ViewerInstance.canZoomOut() ? $(".btnZoomOut").removeClass("disabled") : $(".btnZoomOut").addClass("disabled");

			if (zoomMode == WebPDF.ZOOM_NUMBER) {
				$(".btnSelectZoom").val(data.newScale * 100);
			} else {
				$(".btnSelectZoom").val(zoomMode);
			}
			var currentZoomIndex = -1;
			var zoomList = WebPDF.ViewerInstance.getCurrentScaleList();
			for (var i = 0; i < zoomList.length; i++) {
				var zoom = zoomList[i];
				if (zoom == data.newScale) {
					currentZoomIndex = i;
					break;
				}
			}
			$("#zoomSelectSlider").slider('value', currentZoomIndex);
			}).on(WebPDF.EventList.DOCVIEW_PAGE_CHANGED, function(event, data) {
				var bFirstPage = data.curIndex === 0;
				var bLastPage = data.curIndex === WebPDF.ViewerInstance.getPageCount() - 1;
				if (bFirstPage) {
					btnGoToFirstPage.addClass("disabled");				
					btnGoToPreviousPage.addClass("disabled");				
				} else {
					btnGoToFirstPage.removeClass("disabled");					
					btnGoToPreviousPage.removeClass("disabled");					
				}
				if (bLastPage) {
					btnGoToNextPage.addClass("disabled");					
					btnGoToLastPage.addClass("disabled");					
				} else {
					btnGoToNextPage.removeClass("disabled");					
					btnGoToLastPage.removeClass("disabled");					
				}							
				pageIndexInputSelect.val(data.curIndex);	
			})
			.on(WebPDF.EventList.DOCUMENT_LOADED, function(event, data) {
				_loadUIForAppInstance();
			})
			.on(WebPDF.EventList.DOC_MODIFY_STATE_CHANGED, function(event, isModified) {})
			.on("NotFound", function() {})
			.on("RegPlugin", function(event, pluginName) {})
			.on(WebPDF.EventList.PAGE_SHOW_COMPLETE, function(event, data) {})
			.on(WebPDF.EventList.PAGE_SHOW_START, function(event, data) {})
			.on(WebPDF.EventList.PAGE_FORM_SHOW_COMPLETE, function(event, data) {				
			});
 	}

	function _loadUIForAppInstance() {	 	
	    pageIndexInputSelect.empty();
	    if (pageIndexInputSelect.length > 0) {
	        var pageCnt = WebPDF.ViewerInstance.getPageCount();
	        for (var i = 0; i < pageCnt; i++) {
	            pageIndexInputSelect.append("<option value='" + i + "'>" + (i + 1) + " / " + pageCnt + "</option>");
	        }
	    }
	    selectZoom.empty();
	    var zoomList = WebPDF.ViewerInstance.getAvailableZoomList();
	    for (var i = 0; i < zoomList.length; i++) {
	        var obj = zoomList[i];
	        var option = $("<option>").val(obj.key).html(obj.value);
	        selectZoom.append(option);
	    }

	    var curZoom = WebPDF.ViewerInstance.getCurZoom();
	    var zoomMode = WebPDF.ViewerInstance.curZoomMode();
	    if (zoomMode == WebPDF.ZOOM_NUMBER) {
	        selectZoom.val(curZoom * 100);
	    } else {
	        selectZoom.val(zoomMode);
	    }

	    var currentZoomIndex = -1;
	    var zoomList = WebPDF.ViewerInstance.getCurrentScaleList();
	    for (var i = 0; i < zoomList.length; i++) {
	        var zoom = zoomList[i];
	        if (zoom == curZoom) {
	            currentZoomIndex = i;
	            break;
	        }
	    }
	    zoomSelectSlider.slider('value', currentZoomIndex);   
	}

	var webreader = {};
	webreader.initViewerForPreView = function (s3Key) {
		initViewer(s3Key);
	};

	function initViewer(s3Key) {			
	    var webpdf = window.webPdfPath + "/js/reader/release/webpdf.mini.js";   
	    if (typeof(debug) !== "undefined" && debug === true) {
	        webpdf = window.webPdfPath + "/js/reader/core/Viewer.js";      
	    }    	   
	    window.continuous = true;
		require.async(webpdf, function() {
			var options = {
				fileId: WebPDF.makeS3KeyParamter(s3Key),
				url: "/webpdf/",
				baseUrl: "/",
				language: "en-US",
				defaultZoom: WebPDF.ZOOM_FIT_WIDTH,
				scrollBarType: 1,
				defaultDialogTitle: webReaderNormal.Detail.defaultDialogTitle,
				userName: webReaderNormal.Detail.userName,
				leftPanelShowOnStart: false
			};
			var viewer = WebPDF.createViewer("docViewer", options);
			WebPDF.Config.Plugins.load.Edit = true;
			if (window.hideSignaturePlugin) {
				WebPDF.Config.Plugins.load.Signature = false;
			}
			_bindEventsForAppInstance(viewer);
			viewer.load();
		});
	}

	return webreader;
});


